package com.tianji.remark.init;

import com.tianji.remark.constants.RedisConstants;
import com.tianji.remark.domain.po.LikedRecord;
import com.tianji.remark.service.ILikedRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.annotation.PostConstruct;
import java.util.List;

// @Component
@RequiredArgsConstructor
public class RedisDataInitializer {
    private final ILikedRecordService likedRecordService;
    private final StringRedisTemplate redisTemplate;

    @PostConstruct
    public void initRedisData() {
        List<LikedRecord> records = likedRecordService.list();
        for (LikedRecord record : records) {
            String bizKey = RedisConstants.LIKES_BIZ_KEY_PREFIX + record.getBizId();
            // 添加用户到Set
            redisTemplate.opsForSet().add(bizKey, record.getUserId().toString());
            // 存储bizType映射
            redisTemplate.opsForHash().put(
                RedisConstants.BIZ_TYPE_MAPPING_KEY,
                record.getBizId().toString(),
                record.getBizType()
            );
        }
    }
}